using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.ObjectBuilder;
using GC2.ExceptionHandler;
using GC2.ExceptionHandler.Services;
using GC2.Entities.Admin;
using GC2.DataAccess;

namespace GC2.DataAccess.DAAdmin
{
    public class DAPFCompras
    {
        private Database _database;        // Objeto DataBase del Data Access Application Block

        #region Propiedades miembro
        /// <summary>
        /// Seteo la propiedad _database de la clase.
        /// </summary>
        private Database DataBase
        {
            get { return _database; }
            set { _database = value; }
        }
        #endregion

        #region DAPFComprasGet
        /// <summary>
        /// Devuelve lista Registro de PFCompras ( Periodo Fiscal Activo para Compras y Gastos con Impuestos)
        /// </summary>
        /// <param name="Connection"></param>
        /// <returns>PFCompras</returns>
        public PFCompras DAPFComprasGet(string Connection)
        {
            try
            {
                this.DataBase = DatabaseFactory.CreateDatabase(Connection);
                DbCommand cmd = DataBase.GetStoredProcCommand("PFCompras_Get");

                PFCompras pfCompras = new PFCompras();
                using (cmd)
                {
                    IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                    while (Odbreader.Read())
                    {
                        pfCompras.Id = (int)Odbreader["Id"];
                        pfCompras.PFMes = (int)Odbreader["PFMes"];
                        pfCompras.PFAnio = (int)Odbreader["PFAnio"];
                    }
                    Odbreader.Close();
                    Odbreader.Dispose();
                }
                return pfCompras;
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DAPFComprasUpdate
        /// <summary>
        /// Actualiza un PFCompras id 1
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="PFCompras"></param>
        public void DAPFComprasUpdate(string Connection, PFCompras pfCompras)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("PFCompras_Update");

            try
            {
                using (cmd)
                {
                    // DataBase.AddInParameter(cmd, "@Id", DbType.Int16, pfCompras.Id); Solo existe Id = 1
                    DataBase.AddInParameter(cmd, "@PFMes", DbType.Int16, pfCompras.PFMes);
                    DataBase.AddInParameter(cmd, "@PFAnio", DbType.Int16, pfCompras.PFAnio);

                    DataBase.ExecuteNonQuery(cmd);
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion
    }
}
